package com.gz.moreaseManager.sys.controller;

import com.gz.moreaseManager.config.SelfUserDetails;
import com.gz.moreaseManager.sys.entity.Menu;
import com.gz.moreaseManager.sys.service.IMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
public class MainController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	private final IMenuService menuService;

	@Autowired
	public MainController(IMenuService menuService) {
		this.menuService = menuService;
	}

	@GetMapping("/")
	public ModelAndView webIndex(HttpServletRequest request, Authentication authentication) {

		return new ModelAndView("redirect:/web/index");
	}

	/**
	 * 登录界面
	 *
	 * @return 登录模板
	 */
	@GetMapping("/login")
	public ModelAndView login(HttpServletRequest request, Authentication authentication) {

		return new ModelAndView("login");
	}

	/**
	 * 系统主页
	 *
	 * @param authentication 登录用户信息
	 * @return 首页模板
	 */
	@GetMapping("/index")
	public ModelAndView index(Authentication authentication) {
		SelfUserDetails userDetails = (SelfUserDetails) authentication.getPrincipal();
		List<Menu> menuList = menuService.findMenuListByUserId(userDetails.getUserId(), (byte) 1, (byte) 1);
		ModelAndView modelAndView = new ModelAndView("index");
		modelAndView.addObject("user", userDetails);
		modelAndView.addObject("menuList", menuList);
		return modelAndView;
		//return new ModelAndView("redirect:/web/index");
	}

	/**
	 * 首页模板
	 *
	 * @param authentication 登录用户信息
	 * @return 首页模板
	 */
	@GetMapping("/main")
	public ModelAndView main(Authentication authentication) {
		SelfUserDetails userDetails = (SelfUserDetails) authentication.getPrincipal();
		return new ModelAndView("main", "user", userDetails);
	}

	/**
	 * 403-访问拒绝
	 *
	 * @return 错误页面
	 */
	@GetMapping("/error/403")
	public ModelAndView error403() {
		return new ModelAndView("error/403");
	}

	/**
	 * 404-找不到页面
	 *
	 * @return 错误页面
	 */
	@GetMapping("/error/404")
	public ModelAndView error404() {
		return new ModelAndView("error/404");
	}

	/**
	 * 500-错误页面
	 *
	 * @return 错误页面
	 */
	@GetMapping("/error/500")
	public ModelAndView error500() {
		return new ModelAndView("error/500");
	}
}
